using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces;
using UwinEducation.Application.Dtos;
using UwinEducation.Shared.Enums;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 学生课表详情管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("student.order")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class StudentScheduleDetailsController : ControllerBase
    {
        private readonly IStudentScheduleDetailsService _studentScheduleDetailsService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="studentScheduleDetailsService">学生课表详情服务</param>
        public StudentScheduleDetailsController(IStudentScheduleDetailsService studentScheduleDetailsService)
        {
            _studentScheduleDetailsService = studentScheduleDetailsService;
        }

        /// <summary>
        /// 获取学生课表详情
        /// </summary>
        /// <param name="request">学生课表详情ID请求参数</param>
        /// <returns>学生课表详情</returns>
        [HttpPost]
        public async Task<ApiResponse<StudentScheduleDetailsDto>> GetById([FromBody] IdRequest request)
        {
            var detail = await _studentScheduleDetailsService.GetByIdAsync(request.Id);
            return ApiResponse<StudentScheduleDetailsDto>.Ok(detail);
        }

        /// <summary>
        /// 获取订单详情下的所有课表详情
        /// </summary>
        /// <param name="request">订单详情ID请求参数</param>
        /// <returns>学生课表详情列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<StudentScheduleDetailsDto>>> GetByOrderDetailId([FromBody] IdRequest request)
        {
            var details = await _studentScheduleDetailsService.GetByOrderDetailIdAsync(request.Id);
            return ApiResponse<List<StudentScheduleDetailsDto>>.Ok(details);
        }

        /// <summary>
        /// 获取教师课表详情下的所有学生课表
        /// </summary>
        /// <param name="request">教师课表详情ID请求参数</param>
        /// <returns>学生课表详情列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<StudentScheduleDetailsDto>>> GetByTeacherScheduleDetailsId([FromBody] IdRequest request)
        {
            var details = await _studentScheduleDetailsService.GetByTeacherScheduleDetailsIdAsync(request.Id);
            return ApiResponse<List<StudentScheduleDetailsDto>>.Ok(details);
        }

        /// <summary>
        /// 根据状态获取学生课表详情
        /// </summary>
        /// <param name="status">课表状态</param>
        /// <returns>学生课表详情列表</returns>
        [HttpGet]
        public async Task<ApiResponse<List<StudentScheduleDetailsDto>>> GetByStatus(ScheduleStatus status)
        {
            var details = await _studentScheduleDetailsService.GetByStatusAsync(status);
            return ApiResponse<List<StudentScheduleDetailsDto>>.Ok(details);
        }

        /// <summary>
        /// 获取指定时间范围内的学生课表详情
        /// </summary>
        /// <param name="request">日期范围请求参数</param>
        /// <returns>学生课表详情列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<StudentScheduleDetailsDto>>> GetByDateRange([FromBody] StudentScheduleDateRangeRequest request)
        {
            var details = await _studentScheduleDetailsService.GetByDateRangeAsync(
                request.OrderDetailId,
                request.StartTime,
                request.EndTime);
            return ApiResponse<List<StudentScheduleDetailsDto>>.Ok(details);
        }

        /// <summary>
        /// 创建学生课表详情
        /// </summary>
        /// <param name="createDto">创建学生课表详情请求参数</param>
        /// <returns>创建的学生课表详情</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<StudentScheduleDetailsDto>> Create([FromBody] CreateStudentScheduleDetailsDto createDto)
        {
            var detail = await _studentScheduleDetailsService.CreateAsync(createDto);
            return ApiResponse<StudentScheduleDetailsDto>.Ok(detail);
        }

        /// <summary>
        /// 批量创建学生课表详情
        /// </summary>
        /// <param name="createDtos">批量创建学生课表详情请求参数</param>
        /// <returns>创建的学生课表详情列表</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<List<StudentScheduleDetailsDto>>> CreateRange([FromBody] List<CreateStudentScheduleDetailsDto> createDtos)
        {
            var details = await _studentScheduleDetailsService.CreateRangeAsync(createDtos);
            return ApiResponse<List<StudentScheduleDetailsDto>>.Ok(details);
        }

        /// <summary>
        /// 更新学生课表详情
        /// </summary>
        /// <param name="updateDto">更新学生课表详情请求参数</param>
        /// <returns>更新后的学生课表详情</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<StudentScheduleDetailsDto>> Update([FromBody] UpdateStudentScheduleDetailsDto updateDto)
        {
            var detail = await _studentScheduleDetailsService.UpdateAsync(updateDto);
            return ApiResponse<StudentScheduleDetailsDto>.Ok(detail);
        }

        /// <summary>
        /// 更新学生课表状态
        /// </summary>
        /// <param name="request">更新状态请求参数</param>
        /// <returns>更新后的学生课表详情</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<StudentScheduleDetailsDto>> UpdateStatus([FromBody] UpdateScheduleStatusRequest request)
        {
            var detail = await _studentScheduleDetailsService.UpdateStatusAsync(
                request.Id,
                request.Status,
                request.Reason);
            return ApiResponse<StudentScheduleDetailsDto>.Ok(detail);
        }

        /// <summary>
        /// 删除学生课表详情
        /// </summary>
        /// <param name="request">学生课表详情ID请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _studentScheduleDetailsService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(new object());
        }

        /// <summary>
        /// 批量删除学生课表详情
        /// </summary>
        /// <param name="ids">学生课表详情ID列表</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<object>> DeleteRange([FromBody] List<Guid> ids)
        {
            await _studentScheduleDetailsService.DeleteRangeAsync(ids);
            return ApiResponse<object>.Ok(new object());
        }

        /// <summary>
        /// 检查时间冲突
        /// </summary>
        /// <param name="request">时间冲突检查请求参数</param>
        /// <returns>是否存在冲突</returns>
        [HttpPost]
        public async Task<ApiResponse<bool>> HasTimeConflict([FromBody] TimeConflictCheckRequest request)
        {
            var hasConflict = await _studentScheduleDetailsService.HasTimeConflictAsync(
                request.OrderDetailId,
                request.StartTime,
                request.EndTime,
                request.ExcludeId);
            return ApiResponse<bool>.Ok(hasConflict);
        }

        /// <summary>
        /// 分页查询学生课表详情
        /// </summary>
        /// <param name="request">分页查询请求参数</param>
        /// <returns>分页查询结果</returns>
        [HttpPost]  
        public async Task<ApiResponse<PagedResult<StudentScheduleDetailsDto>>> GetPaged([FromBody] StudentScheduleDetailsPageRequest request)
        {
            var result = await _studentScheduleDetailsService.GetPagedAsync(request);
            return ApiResponse<PagedResult<StudentScheduleDetailsDto>>.Ok(result);
        }

        /// <summary>
        /// 获取教师课表集合
        /// </summary>
        /// <param name="request">教师课表集合请求参数</param>
        /// <returns>教师课表集合</returns>
        [HttpPost]
        public async Task<ApiResponse<List<TeacherScheduleListDto>>> GetTeacherScheduleList([FromBody] TeacherScheduleListRequest request)
        {
            var result = await _studentScheduleDetailsService.GetTeacherScheduleListAsync(request);
            return ApiResponse<List<TeacherScheduleListDto>>.Ok(result);
        }


    }

    /// <summary>
    /// 学生课表日期范围请求参数
    /// </summary>
    public class StudentScheduleDateRangeRequest
    {
        /// <summary>
        /// 订单详情ID
        /// </summary>
        public Guid OrderDetailId { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }
    }

    /// <summary>
    /// 更新课表状态请求参数
    /// </summary>
    public class UpdateScheduleStatusRequest
    {
        /// <summary>
        /// 学生课表详情ID
        /// </summary>
        public Guid Id { get; set; }

        /// <summary>
        /// 课表状态
        /// </summary>
        public ScheduleStatus Status { get; set; }

        /// <summary>
        /// 状态变更原因
        /// </summary>
        public string Reason { get; set; }
    }

    /// <summary>
    /// 时间冲突检查请求参数
    /// </summary>
    public class TimeConflictCheckRequest
    {
        /// <summary>
        /// 订单详情ID
        /// </summary>
        public Guid OrderDetailId { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 排除的ID（可选）
        /// </summary>
        public Guid? ExcludeId { get; set; }
    }
} 